package com.xsh.gls.interceptor;

import com.xsh.gls.pojo.User;
import com.xsh.gls.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
    public class MyHandlerInterceptor implements HandlerInterceptor{
    @Autowired
    RedisUtils redisUtils;
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler)throws Exception{
            System.out.println("拦截器");
            response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
            response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
            response.setHeader("Access-Control-Max-Age", "3600");
            response.setHeader("Access-Control-Allow-Headers", "x-requested-with");
            response.setHeader("Access-Control-Allow-Credentials", "true");
            String token=request.getParameter("token");
            if(token==null||token.equals("")){
                return false;
            }
            Object user=redisUtils.get(token);
            request.setAttribute("user",user);
            return true;

    }


}
